﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TimeQuest.Base
{
    public class GameObject
    {
        #region Constructors

        public GameObject()
        {
            GameObjectID = Guid.NewGuid();
            BlocksPlayer = true;
        }

        #endregion

        #region Properties

        public bool BlocksPlayer { get; set; }
        public int GameObjectTextureID { get; set; }
        public int BaseYoffset { get; set; }
        public int BaseXoffset { get; set; }
        public int CenterOfBaseY
        {
            get
            {
                return BaseRectangle.Y + (BaseRectangle.Height / 2);
            }
        }
        public int CenterOfBaseX
        {
            get
            {
                return BaseRectangle.X + (BaseRectangle.Width / 2);
            }
        }
        public Point Location;
        public Guid GameObjectID { get; set; }

        public event EventHandler OnClick;

        public virtual Rectangle BaseRectangle
        {
            get
            {
                if (GameObjectTextureID != 0)
                {
                    Texture2D t = ContentHandler.GetTexture(GameObjectTextureID);
                    int Width = BaseWidth;
                    int RemovedWidth = t.Width - Width;
                    //int x = Location.X + (RemovedWidth / 2) + BaseXoffset;
                    int x = Location.X + BaseXoffset;
                    int w = Width;

                    int Height = BaseHeight;
                    int RemovedHeight = t.Height - Height;
                    //int y = Location.Y + (RemovedWidth / 2) + BaseYoffset;
                    int y = Location.Y + BaseYoffset;
                    int h = Height;

                    return new Rectangle(x, y, w, h);
                }
                else
                    return Rectangle.Empty;
            }
        }
        public virtual int BaseWidth { get; set; }
        public virtual int BaseHeight { get; set; }

        #endregion

        public virtual void Draw(SpriteBatch sb)
        {
            Texture2D t = ContentHandler.GetTexture(GameObjectTextureID);
            sb.Draw(t, new Rectangle(Location.X, Location.Y, t.Width, t.Height), Color.White);
            if (GameWorld.ShowBlockingRectangles)
            {
                sb.Draw(ContentHandler.GetTexture(3), BaseRectangle, Rectangle.Empty, new Color(Color.Red, .3f));
                sb.Draw(ContentHandler.GetTexture(3), new Rectangle(BaseRectangle.X, CenterOfBaseY, BaseRectangle.Width, 1), Rectangle.Empty, Color.Red);
            }
        }

        public virtual void Update(GameTime gt)
        {
        }

        #region Helpers

        internal void InvokeOnClick(object sender, EventArgs e)
        {
            if (OnClick != null)
                OnClick(sender, e);
        }
        public int RoundUp(decimal value)
        {
            return int.Parse(Math.Round(value, 0, MidpointRounding.AwayFromZero).ToString());
        }

        #endregion
    }
}
